<?php
$sMsg="";
$Action="?mod=admin.add&method=add";
$Submit="Đồng ý";
$TitleTable="Tạo mới";
if ($Method=="add")
{
	if(isset($_POST["TenDN"])) {$TenDN=addslashes($_POST["TenDN"]);}
	if(isset($_POST["MatKhau"])) $MatKhau=md5($_POST["MatKhau"]);
	if(isset($_POST["Email"])) $Email=$_POST["Email"];
	if(isset($_POST["Active"])) $Active=$_POST["Active"];
	if (trim($TenDN)!="")
	{
			$sql="Insert into ".$prefix."admin(TenDN,MatKhau,Email,Active) values('$TenDN','$MatKhau','$Email',$Active)";
			//echo $sql;
			mysql_query($sql);
			Redirect("Đã thêm mới thành công","?mod=admin");
	} 
}
if($Method=="edit")
{
	$id=$_GET["id"];
	$sql="select * from ".$prefix."admin where ID=$id";
	$result=mysql_query($sql);
	if(mysql_num_rows($result))
	{
		$rows=mysql_fetch_array($result);
		$TenDN=stripslashes($rows["TenDN"]);
		$Active=$rows["Active"];
		$Email=$rows["Email"];
		$Submit="Cập nhập";
		$TitleTable="Sửa thông tin";
		$Action="?mod=admin.add&method=update&id=$id";
	}
}

if ($Method=="update")
{
	$Submit="Tạo Mới";
	$id=$_GET["id"];
	if(isset($_POST["TenDN"])) {$TenDN=addslashes($_POST["TenDN"]);}
	if(isset($_POST["MatKhau"])) $MatKhau=md5($_POST["MatKhau"]);
	if(isset($_POST["Email"])) $Email=$_POST["Email"];
	if(isset($_POST["Active"])) $Active=$_POST["Active"];
	if (trim($TenDN)!="")
	{
			$sql="update ".$prefix."admin set TenDN='$TenDN',Email='$Email',Active=$Active,MatKhau='$MatKhau' where ID=$id";
			mysql_query($sql);
			Redirect("Cập nhập thành công","?mod=admin");
	} 
}
?>
<script language="javascript1.2">
	function CheckFrm(_form)
	{
	if (_form.TenDN.value==""){alert("Bạn phải nhập tên đăng nhập");_form.TenDN.focus();return false;}
	if (_form.MatKhau.value==""){alert("Bạn phải nhập mật khẩu admin");_form.MatKhau.focus();return false;}
	if (_form.Email.value==""){alert("Bạn phải nhập Email");_form.Email.focus();return false;}
	if(_form.MatKhau.value!=_form.ReMatKhau.value)
	{
		alert("Mật khẩu không khớp nhau !");
		return false;
	}
	if (_form.Active.value==""){alert("Bạn phải chọn trạng thái admin");_form.Active.focus();return false;}
	}
</script>

<!--Bat dau form tao sua -->
<form name="frmHuyen" id="frmHuyen" action="<?=$Action;?>" method="post" onSubmit="return CheckFrm(this);">
	<table width="100%" border="1" style="border-collapse:collapse " bordercolor="#B2DAEE">
	  <tr class="newsList-content-head">
		<td height="28" colspan="2" align="center"><?=$TitleTable?></td>
	  </tr>		  
	  <tr>
	      <td width="254" align="right"><strong>
		  Tên đăng nhập :</strong></td>
	      <td width="497">
	      <input type="text" name="TenDN" value="<?=$TenDN;?>" size="40" class="TextBox"> *	      </td>
	  </tr>
	  <tr>
		<td width="254" align="right"><strong>Mật khẩu :</strong></td>
		<td width="497">
		<input type="password" name="MatKhau" value="<?=$MatKhau;?>" size="40" class="TextBox"> *		</td>
	  </tr>
	  <tr>
		<td width="254" align="right"><strong>Nhập lại mật khẩu :</strong></td>
		<td width="497">
		<input type="password" name="ReMatKhau" value="<?=$MatKhau;?>" size="40" class="TextBox"> *		</td>
	  </tr>	  
	  <tr>
		<td width="254" align="right"><strong>Email:</strong></td>
		<td width="497">
		<input type="text" name="Email" value="<?=$Email?>" size="40" class="TextBox">*		</td>
	  </tr>
	  <tr>
		<td width="254" align="right"><strong>Kích hoạt:</strong></td>
		<td width="497">
		<select name="Active" id="Active" style="width:100px">
		<option value="1">Kích hoạt</option>
		<option value="0">Không</option>
		</select>
			<script language="javascript">
		 for(var i=0;i<document.frmHuyen.Active.length;i++)
		 {if(document.frmHuyen.Active[i].value=="<?=$Active?>")
		 {document.frmHuyen.Active.selectedIndex=i;break;}}
		</script>
		</td>
	  </tr>
	  <tr>
		<td colspan="2" align="center">
		<input type="submit" name="Submit" value="<?=$Submit?>" class="Button" onClick="return CheckFrmTl();">&nbsp;&nbsp;&nbsp;
		<input type="reset" name="reset" value="Làm lại" class="Button">&nbsp;&nbsp;&nbsp;
		<input type="button" name="back" value="Quay về" class="Button" onclick="GoBack('?mod=admin')"></td>
	  </tr>
	</table>
</form>
	